package com.sh.caryr.erp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sh.caryr.erp.entity.product.CombDetail;
import com.sh.caryr.erp.entity.product.CombNum;
import com.sh.caryr.erp.entity.stock.SkuRecord;
import com.sh.caryr.erp.mapper.CombDetailMapper;
import com.sh.caryr.erp.service.ICombDetailService;
import com.sh.caryr.erp.service.ICombNumService;
import com.sh.caryr.erp.service.ISkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 套件详情表 服务实现类
 * </p>
 *
 * @author milkeeper
 * @since 2017-11-22
 */
@Service
public class CombDetailServiceImpl extends ServiceImpl<CombDetailMapper, CombDetail> implements ICombDetailService {

    @Autowired
    private CombDetailMapper combDetailMapper;

    @Autowired
    private ICombNumService combNumService;

    @Override
    public void getCombDetail(CombDetail combDetail) {
        List<CombNum> combNumList = combNumService.selectList(new EntityWrapper<CombNum>().eq("combDetailId", combDetail.getId()));
        combDetail.setCombNumList(combNumList);
    }

    @Override
    public CombDetail getCombDetailById(Long id) {
        CombDetail combDetail = combDetailMapper.selectById(id);
        this.getCombDetail(combDetail);
        return combDetail;
    }

    @Transactional
    @Override
    public boolean deleteCombDetailById(Long id) {
        combNumService.delete(new EntityWrapper<CombNum>().eq("combDetailId", id));
        return this.deleteById(id);
    }

    @Override
    public void updateSkuLockedAmountById(SkuRecord skuRecord, Long id, Integer combAmount) {
        for (CombNum combNum : this.getCombDetailById(id).getCombNumList()){
            //skuService.updateSkuLockedAmountById(skuRecord, combNum.getSkuId(),combNum.getSkuNum() * combAmount);
        }
    }

    @Override
    public void updateSkuTotalAmountAndLockedAmountById(SkuRecord skuRecord, Long id, Integer combAmount) {
        for (CombNum combNum : this.getCombDetailById(id).getCombNumList()){
            //skuService.updateSkuTotalAmountAndLockedAmountById(skuRecord, combNum.getSkuId(),combNum.getSkuNum() * combAmount);
        }
    }
}
